package com.isman.placeslist;

import java.lang.reflect.Type;
import java.util.ArrayList;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.isman.placeslist.apiobject.People;
import com.isman.placeslist.database.PeopleContent;
import com.isman.placeslist.util.Util;
import com.isman.placeslist.ws.AsyncRunnerHeadler;
import com.isman.placeslist.ws.ServiceProvider;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class PagePeopleFind extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.page_people_find);
		cr = getContentResolver();
		init();
	}

	private void init() {
		vImgImage = (ImageView) findViewById(R.id.imageView1);
		vBtnSync = (Button) findViewById(R.id.btn_find_sync);
		vBtnLoad = (Button) findViewById(R.id.btn_find_firstload);

		vBtnLoad.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				serviceFirstLoadPeople();
			}
		});
		vBtnSync.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {

			}
		});
	}

	private void firstLoadPeople(ArrayList<People> list) {
		cr.delete(PeopleContent.CONTENT_URI, "", null);
		ContentValues value;
		for (People people : list) {
			byte[] data = Util.LoadImageFromWebOperations(people.getUser()
					.getPicture());

			value = new ContentValues();
			value.put(PeopleContent.PID, people.getId());
			value.put(PeopleContent.PSTATUS, people.getStatus());
			value.put(PeopleContent.PCREATEBY, people.getCreateBy());
			value.put(PeopleContent.PMESSAGE, people.getMessage());
			value.put(PeopleContent.UID, people.getUser().getId());
			value.put(PeopleContent.UNAME, people.getUser().getName());
			value.put(PeopleContent.UEMAIL, people.getUser().getEmail());
			value.put(PeopleContent.UTELEPHONE, people.getUser().getTelephone());
			value.put(PeopleContent.UPICTURE, data);
			Log.e("TAF", "" + value.getAsLong(PeopleContent.UID));
			cr.insert(PeopleContent.CONTENT_URI, value);
		}
	}

	private void serviceFirstLoadPeople() {
		ServiceProvider provider = new ServiceProvider(this);
		provider.loadPeople(new AsyncRunnerHeadler() {
			@Override
			public void onComplete(String json) {
				Type type = new TypeToken<ArrayList<People>>() {
				}.getType();
				ArrayList<People> list = new Gson().fromJson(json, type);
				firstLoadPeople(list);
			}

			@Override
			public void onAuthenticate(String json) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onError(int statusCode, String json) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onConnectingError(String message) {
				// TODO Auto-generated method stub

			}
		});
	}

	private ContentResolver cr;

	// vBtnSync.setOnClickListener(new OnClickListener() {
	// @Override
	// public void onClick(View v) {
	// //LoadImageFromWebOperations("http://www.google.co.th/images/nav_logo101.png");
	// ContentResolver mContentResolver = getContentResolver();
	// // Cursor cursor = mContentResolver.query(PeopleContent.CONTENT_URI,
	// null, null, null, "");
	// // DBHelper dbHelper = new DBHelper(PagePeopleFind.this);
	// // SQLiteDatabase read = dbHelper.getReadableDatabase();
	// Cursor cursor = mContentResolver.query(PeopleContent.CONTENT_URI, null,
	// "", null, null);//.query(PeopleContent.TABLE_NAME, new
	// String[]{PeopleContent.UNAME,PeopleContent.UPICTURE}, null, null, null,
	// null, null);
	//
	//
	// // int index = cursor.getColumnIndexOrThrow(PeopleContent.UPICTURE);
	// // byte[] data = cursor.getBlob(cursor.get);
	// cursor.moveToLast();
	// byte[] data =
	// cursor.getBlob(cursor.getColumnIndex(PeopleContent.UPICTURE));
	// // Cursor cur = cr.query(PeopleContent.CONTENT_URI, null, null, null,
	// null);
	// // int index = cur.getColumnIndexOrThrow(PeopleContent.UPICTURE);
	// // byte[] data = cur.getBlob(index);
	// Bitmap mp = BitmapFactory.decodeByteArray(data, 0, data.length);
	// vImgImage.setImageBitmap(mp);
	// }
	// });
	//
	//
	// //
	// LoadImageFromWebOperations("http://www.google.co.th/images/nav_logo101.png");
	//
	// // ContentResolver mContentResolver = getContentResolver();
	// // Cursor cursor = mContentResolver.query(PeopleContent.CONTENT_URI,
	// null, null, null, "");
	// //
	// // int index = cursor.getColumnIndexOrThrow(PeopleContent.UPICTURE);
	// // byte[] data = cursor.getBlob(index);
	// // Bitmap bmp = BitmapFactory.decodeResource(getResources(),
	// R.drawable.ex_desert);
	// //
	// // ByteArrayOutputStream stream = new ByteArrayOutputStream();
	// // bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
	// // byte[] byteArray = stream.toByteArray();
	// //
	// //
	// // ContentValues values = new ContentValues();
	// // values.put(PeopleContent.PID, 11);
	// // values.put(PeopleContent.PSTATUS, 1);
	// // values.put(PeopleContent.PMESSAGE, "message");
	// // values.put(PeopleContent.PCREATEBY, 14);
	// //
	// // values.put(PeopleContent.UID, 1);
	// // values.put(PeopleContent.UNAME,"Isman");
	// // values.put(PeopleContent.UEMAIL, "email@gmail.com");
	// // values.put(PeopleContent.UTELEPHONE, "12345678");
	// // values.put(PeopleContent.UPICTURE, byteArray);
	// // DBHelper dbHelper = new DBHelper(this);
	// //
	// // SQLiteDatabase s= dbHelper.getWritableDatabase();
	// // s.insert(PeopleContent.TABLE_NAME, "", values);
	//
	// }
	//
	//
	// Bitmap defaultIcon = BitmapFactory.decodeStream(is);
	//
	// vImgImage.setImageBitmap(defaultIcon);
	//
	//
	// ByteArrayOutputStream stream = new ByteArrayOutputStream();
	// defaultIcon.compress(Bitmap.CompressFormat.JPEG, 100, stream);
	//
	//
	// // Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
	// // ByteArrayOutputStream stream = new ByteArrayOutputStream();
	// // bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bitmap);
	// // byte[] bitmapdata = stream.toByteArray();
	// // return d;
	//
	//
	// ContentValues values = new ContentValues();
	// values.put(PeopleContent.PID, 11);
	// values.put(PeopleContent.PSTATUS, 1);
	// values.put(PeopleContent.PMESSAGE, "message");
	// values.put(PeopleContent.PCREATEBY, 14);
	//
	// values.put(PeopleContent.UID, 1);
	// values.put(PeopleContent.UNAME,"Isman");
	// values.put(PeopleContent.UEMAIL, "email@gmail.com");
	// values.put(PeopleContent.UTELEPHONE, "12345678");
	// values.put(PeopleContent.UPICTURE, stream.toByteArray());
	// DBHelper dbHelper = new DBHelper(this);
	//
	// SQLiteDatabase s= dbHelper.getWritableDatabase();
	// s.insert(PeopleContent.TABLE_NAME, "", values);
	// }catch (Exception e) {
	// System.out.println("Exc="+e);
	//
	// }
	// }
	// private void mSync(){
	// ServiceProvider provider = new ServiceProvider(this);
	// provider.SyncPeople(new AsyncRunnerHeadler() {
	// @Override
	// public void onComplete(String json) {
	// super.onComplete(json);
	// Type type = new TypeToken<ArrayList<People>>(){}.getType();
	// ArrayList<People> list = new Gson().fromJson(json, type);
	// ContentResolver cr = getContentResolver();
	//
	// for (People people : list) {
	// ContentValues values = new ContentValues();
	// values.put(PeopleContent.PID, people.getId());
	// values.put(PeopleContent.PSTATUS, people.getStatus());
	// values.put(PeopleContent.PMESSAGE, people.getMessage());
	// values.put(PeopleContent.PCREATEBY, people.getCreateBy());
	//
	// values.put(PeopleContent.UID, people.getUser().getId());
	// values.put(PeopleContent.UNAME, people.getUser().getId());
	// values.put(PeopleContent.UEMAIL, people.getUser().getId());
	// values.put(PeopleContent.UTELEPHONE, people.getUser().getId());
	// cr.insert(PeopleContent.CONTENT_URI, values);
	// }
	// }
	// });
	// }

	private ImageView vImgImage;
	private Button vBtnSync;
	private Button vBtnLoad;
}
